/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iie.main;

import iie.tools.JettyTools;
import iie.tools.PropsFiles;
import iie.tools.XmlTools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.dom4j.DocumentException;

/**
 *
 * @author zhangyun
 */
public class ProcedureManager {

    /**
     * @param args the command line arguments
     */
    private static Logger logger = Logger.getLogger(ProcedureManager.class);
    public static String confPath;
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        ProcedureManager.confPath = args[0];
//        ProcedureManager.confPath = "E:\\数据接入\\maven\\procedureManager\\src\\main\\resources";

        PropertyConfigurator.configure(ProcedureManager.confPath + "/log4j.properties");
        
        logger.info("the xmlFilePath is " + PropsFiles.getValue("xmlFilePath"));
        String xmlnames = PropsFiles.getValue("xmlFilePath");
        String[] xmlname = xmlnames.split("\\|");
        //读取配置文件
        for (String filename : xmlname) {
            logger.info("one: " + filename);
            
            File xmlFile = new File(filename);
            Reader reader = null;
            String xml = "";
            try {
                reader = new InputStreamReader(new FileInputStream(xmlFile));
                int tempchar;
                while ((tempchar = reader.read()) != -1) {
                    xml = xml + (char) tempchar;
                }
                logger.info(xml);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            } finally {
                try {
                    reader.close();
                } catch (Exception e) {
                    logger.error(e.getMessage(), e);
                }
            }
            //获取ftpList
            ArrayList<HashMap<String, String>> ftpList = new ArrayList<HashMap<String, String>>();
            try {
                ftpList = XmlTools.getIterationElementAttributeValue(xml, "operator", "parameters", "ftplist", "ftp");
            } catch (DocumentException ex) {
                logger.error("获取ftpList信息失败。" + ex.getMessage(), ex);
            }

            //解析ftpList
            String sendXml = "";
            if (!ftpList.isEmpty()) {
                //删除ftpList
                xml = XmlTools.rmElement3(xml, "operator", "parameters", "ftplist");
                //添加2级节点
                xml = XmlTools.addElement2(xml, "operator", "ftps", "#####");
                
                for (int i = 0; i < ftpList.size(); i++) {
                    String ftps = "";
                    String ftpip = "";
                    String filepath = "";
                    HashMap<String, String> f = (HashMap<String, String>) ftpList.get(i);
                    
                    for (String str : f.keySet()) {
                        if (str.equals("ip")) {
                            ftpip = f.get(str);
                        } else if (str.equals("filepath")) {
                            filepath = f.get(str);
                        }
                        ftps += "<ftp name=\"" + str + "\">" + f.get(str) + "</ftp>\n";
                    }
                    String[] str = filepath.split("/");
                    String namespace = str[str.length - 1];//0_shipin
                    sendXml = xml;
                    sendXml = sendXml.replace("<ftps>#####</ftps>", "<ftps>" + ftps + "</ftps>");
                    sendXml = sendXml.replace("<parameter name=\"name\">ftpdownload</parameter>", "<parameter name=\"name\">" + ftpip + "_" + namespace + "</parameter>");
                    //确定唯一subjobid
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    logger.info("the uuid is: " + uuid);
                    String subjobid = XmlTools.getElValueFromStr(sendXml, "subjobid");
                    sendXml = sendXml.replace("<subjobid>" + subjobid + "</subjobid>", "<subjobid>" + uuid + "</subjobid>");
                    
                    logger.info("*****the send xml is: " + sendXml);
                    //发送
                    JettyTools.send(sendXml);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ex) {
                        logger.error(ex.getMessage(), ex);
                    }
                }
            }
        }
    }
}
